using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SubSonic.Security.Data
{
	/// <summary>
	/// Strongly-typed collection for the User class.
	/// </summary>
	[Serializable]
	public partial class UserCollection : ActiveList<User, UserCollection> 
	{	   
		public UserCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Users table.
	/// </summary>
	[Serializable]
	public partial class User : ActiveRecord<User>
	{
		#region .ctors and Default Settings
		
		public User()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public User(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public User(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public User(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Users", TableType.Table, DataService.GetInstance("SubSonicProviders"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarUserID = new TableSchema.TableColumn(schema);
				colvarUserID.ColumnName = "UserID";
				colvarUserID.DataType = DbType.Guid;
				colvarUserID.MaxLength = 0;
				colvarUserID.AutoIncrement = false;
				colvarUserID.IsNullable = false;
				colvarUserID.IsPrimaryKey = true;
				colvarUserID.IsForeignKey = false;
				colvarUserID.IsReadOnly = false;
				colvarUserID.DefaultSetting = @"";
				colvarUserID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserID);
				
				TableSchema.TableColumn colvarUsername = new TableSchema.TableColumn(schema);
				colvarUsername.ColumnName = "Username";
				colvarUsername.DataType = DbType.String;
				colvarUsername.MaxLength = -1;
				colvarUsername.AutoIncrement = false;
				colvarUsername.IsNullable = false;
				colvarUsername.IsPrimaryKey = false;
				colvarUsername.IsForeignKey = false;
				colvarUsername.IsReadOnly = false;
				colvarUsername.DefaultSetting = @"";
				colvarUsername.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUsername);
				
				TableSchema.TableColumn colvarPassword = new TableSchema.TableColumn(schema);
				colvarPassword.ColumnName = "Password";
				colvarPassword.DataType = DbType.String;
				colvarPassword.MaxLength = -1;
				colvarPassword.AutoIncrement = false;
				colvarPassword.IsNullable = false;
				colvarPassword.IsPrimaryKey = false;
				colvarPassword.IsForeignKey = false;
				colvarPassword.IsReadOnly = false;
				colvarPassword.DefaultSetting = @"";
				colvarPassword.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPassword);
				
				TableSchema.TableColumn colvarLastLogin = new TableSchema.TableColumn(schema);
				colvarLastLogin.ColumnName = "LastLogin";
				colvarLastLogin.DataType = DbType.DateTime;
				colvarLastLogin.MaxLength = 0;
				colvarLastLogin.AutoIncrement = false;
				colvarLastLogin.IsNullable = false;
				colvarLastLogin.IsPrimaryKey = false;
				colvarLastLogin.IsForeignKey = false;
				colvarLastLogin.IsReadOnly = false;
				colvarLastLogin.DefaultSetting = @"";
				colvarLastLogin.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLastLogin);
				
				TableSchema.TableColumn colvarDateCreated = new TableSchema.TableColumn(schema);
				colvarDateCreated.ColumnName = "DateCreated";
				colvarDateCreated.DataType = DbType.DateTime;
				colvarDateCreated.MaxLength = 0;
				colvarDateCreated.AutoIncrement = false;
				colvarDateCreated.IsNullable = false;
				colvarDateCreated.IsPrimaryKey = false;
				colvarDateCreated.IsForeignKey = false;
				colvarDateCreated.IsReadOnly = false;
				colvarDateCreated.DefaultSetting = @"";
				colvarDateCreated.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateCreated);
				
				TableSchema.TableColumn colvarEmail = new TableSchema.TableColumn(schema);
				colvarEmail.ColumnName = "Email";
				colvarEmail.DataType = DbType.String;
				colvarEmail.MaxLength = -1;
				colvarEmail.AutoIncrement = false;
				colvarEmail.IsNullable = false;
				colvarEmail.IsPrimaryKey = false;
				colvarEmail.IsForeignKey = false;
				colvarEmail.IsReadOnly = false;
				colvarEmail.DefaultSetting = @"";
				colvarEmail.ForeignKeyTableName = "";
				schema.Columns.Add(colvarEmail);
				
				TableSchema.TableColumn colvarSecurityQuestion = new TableSchema.TableColumn(schema);
				colvarSecurityQuestion.ColumnName = "SecurityQuestion";
				colvarSecurityQuestion.DataType = DbType.String;
				colvarSecurityQuestion.MaxLength = -1;
				colvarSecurityQuestion.AutoIncrement = false;
				colvarSecurityQuestion.IsNullable = true;
				colvarSecurityQuestion.IsPrimaryKey = false;
				colvarSecurityQuestion.IsForeignKey = false;
				colvarSecurityQuestion.IsReadOnly = false;
				colvarSecurityQuestion.DefaultSetting = @"";
				colvarSecurityQuestion.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSecurityQuestion);
				
				TableSchema.TableColumn colvarSecurityAnswer = new TableSchema.TableColumn(schema);
				colvarSecurityAnswer.ColumnName = "SecurityAnswer";
				colvarSecurityAnswer.DataType = DbType.String;
				colvarSecurityAnswer.MaxLength = -1;
				colvarSecurityAnswer.AutoIncrement = false;
				colvarSecurityAnswer.IsNullable = true;
				colvarSecurityAnswer.IsPrimaryKey = false;
				colvarSecurityAnswer.IsForeignKey = false;
				colvarSecurityAnswer.IsReadOnly = false;
				colvarSecurityAnswer.DefaultSetting = @"";
				colvarSecurityAnswer.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSecurityAnswer);
				
				TableSchema.TableColumn colvarIsValid = new TableSchema.TableColumn(schema);
				colvarIsValid.ColumnName = "IsValid";
				colvarIsValid.DataType = DbType.Boolean;
				colvarIsValid.MaxLength = 0;
				colvarIsValid.AutoIncrement = false;
				colvarIsValid.IsNullable = false;
				colvarIsValid.IsPrimaryKey = false;
				colvarIsValid.IsForeignKey = false;
				colvarIsValid.IsReadOnly = false;
				colvarIsValid.DefaultSetting = @"";
				colvarIsValid.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsValid);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SubSonicProviders"].AddSchema("Users",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("UserID")]
		public Guid UserID 
		{
			get { return GetColumnValue<Guid>("UserID"); }

			set { SetColumnValue("UserID", value); }

		}

		  
		[XmlAttribute("Username")]
		public string Username 
		{
			get { return GetColumnValue<string>("Username"); }

			set { SetColumnValue("Username", value); }

		}

		  
		[XmlAttribute("Password")]
		public string Password 
		{
			get { return GetColumnValue<string>("Password"); }

			set { SetColumnValue("Password", value); }

		}

		  
		[XmlAttribute("LastLogin")]
		public DateTime LastLogin 
		{
			get { return GetColumnValue<DateTime>("LastLogin"); }

			set { SetColumnValue("LastLogin", value); }

		}

		  
		[XmlAttribute("DateCreated")]
		public DateTime DateCreated 
		{
			get { return GetColumnValue<DateTime>("DateCreated"); }

			set { SetColumnValue("DateCreated", value); }

		}

		  
		[XmlAttribute("Email")]
		public string Email 
		{
			get { return GetColumnValue<string>("Email"); }

			set { SetColumnValue("Email", value); }

		}

		  
		[XmlAttribute("SecurityQuestion")]
		public string SecurityQuestion 
		{
			get { return GetColumnValue<string>("SecurityQuestion"); }

			set { SetColumnValue("SecurityQuestion", value); }

		}

		  
		[XmlAttribute("SecurityAnswer")]
		public string SecurityAnswer 
		{
			get { return GetColumnValue<string>("SecurityAnswer"); }

			set { SetColumnValue("SecurityAnswer", value); }

		}

		  
		[XmlAttribute("IsValid")]
		public bool IsValid 
		{
			get { return GetColumnValue<bool>("IsValid"); }

			set { SetColumnValue("IsValid", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public SubSonic.Security.Data.UsersInRoleCollection UsersInRoles()
		{
			return new SubSonic.Security.Data.UsersInRoleCollection().Where(UsersInRole.Columns.UserID, UserID).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varUserID,string varUsername,string varPassword,DateTime varLastLogin,DateTime varDateCreated,string varEmail,string varSecurityQuestion,string varSecurityAnswer,bool varIsValid)
		{
			User item = new User();
			
			item.UserID = varUserID;
			
			item.Username = varUsername;
			
			item.Password = varPassword;
			
			item.LastLogin = varLastLogin;
			
			item.DateCreated = varDateCreated;
			
			item.Email = varEmail;
			
			item.SecurityQuestion = varSecurityQuestion;
			
			item.SecurityAnswer = varSecurityAnswer;
			
			item.IsValid = varIsValid;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varUserID,string varUsername,string varPassword,DateTime varLastLogin,DateTime varDateCreated,string varEmail,string varSecurityQuestion,string varSecurityAnswer,bool varIsValid)
		{
			User item = new User();
			
				item.UserID = varUserID;
				
				item.Username = varUsername;
				
				item.Password = varPassword;
				
				item.LastLogin = varLastLogin;
				
				item.DateCreated = varDateCreated;
				
				item.Email = varEmail;
				
				item.SecurityQuestion = varSecurityQuestion;
				
				item.SecurityAnswer = varSecurityAnswer;
				
				item.IsValid = varIsValid;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string UserID = @"UserID";
			 public static string Username = @"Username";
			 public static string Password = @"Password";
			 public static string LastLogin = @"LastLogin";
			 public static string DateCreated = @"DateCreated";
			 public static string Email = @"Email";
			 public static string SecurityQuestion = @"SecurityQuestion";
			 public static string SecurityAnswer = @"SecurityAnswer";
			 public static string IsValid = @"IsValid";
						
		}

		#endregion
	}

}

